<?php
/**
 * @Description 客户端
 * @Sonkwl Xiong
 * @Date 2023/03/24 14:47:20
 * @GET 
 * 
 * @POST 
 * 
 * @Response 
 *  
 */
namespace Sonkwl\PublishPHP;

class client{
    private static $url="";
    private static $path=".";
    private static $projectname="";
    private static $upmode="COPY";//COPY直接解压到目录，DEL先删除目录文件再解压
    private static $nodirs=array();
    //设置服务器端的url
    public static function SetUrl($url){
        self::$url=$url;
    }
    //设置需要打包发布的项目路径
    public static function SetPath($path){
        self::$path=$path;
    }
    //设置需要打包发布的项目名称
    public static function SetProjectName($projectname){
        self::$projectname=$projectname;
    }
    //设置指定不发布目录
    public static function SetNoDirs($nodirs){
        self::$nodirs=$nodirs;
    }
    public static function SetUpmode($mod){
        if(!in_array($mod,array("COPY","DEL"))){
            echo 'Fail:模式定义错误';
            exit;
        }
        self::$upmode=$mod;
    }
    //打包文件
    public static function Release(){
        if(self::$url==""){
            echo "Fail:服务器url没有定义";
            exit;
        }
        if(!is_dir(self::$path)){
            echo "Fail:项目目录[".self::$path."]不存在";
            exit;
        }
        $files=self::GetFiles(self::$path);
        // print_r($files); 
        // exit;
        $zip=new \ZipArchive;
        $zipname=date("YmdHis").random_int(1,100).".zip";
        if($zip->open($zipname,\ZipArchive::CREATE)===TRUE){
            foreach ($files as $key => $filename) {
                $zip->addFile($filename);
            }
        }
        $zip->close();
        self::Upfile($zipname);
    }
    //获得当前目录所有文件
    public static function GetFiles($dirname){
        $files=array();
        foreach(scandir($dirname) as $file){
            if($file=="." || $file==".."){
                continue;
            }
            //过滤-dev的文件或目录
            if(strpos($file,"-dev")!==false){
                continue;
            }
            $filepath=$dirname."/".$file;
            if($dirname=="."){
                $filepath=$file;
            }
            echo $filepath."\r\n";
            if(is_file($filepath)){
                array_push($files,$filepath);
            }else if(is_dir($filepath)){
                //不压缩目录
                if(count(self::$nodirs)>0){
                    if(in_array($filepath,self::$nodirs)){
                        continue;
                    }
                }
                $_files=self::GetFiles($filepath);
                $files=array_merge($files,$_files);
            }else{
                continue;
            }
        }
        return $files;
    }
    //post上传zip文件
    public static function Upfile($path){
        if(self::$url==""){
            echo 'Fail:远程服务器的URL没有定义';
            exit;
        }
        $file=realpath($path);
        $filedata=array("uploaded_file"=>new \CURLFile($file));
        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,self::$url."?project=".self::$projectname."&mod=".self::$upmode);
        curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"POST");
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$filedata);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $res=curl_exec($ch);
        curl_close($ch);
        unlink($file);
        echo $res;
    }
}
?>